package com.android.porttallymansystem.ui.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.lifecycle.Observer
import com.android.porttallymansystem.R
import com.android.porttallymansystem.databinding.ActivityUserInfoBinding
import com.android.porttallymansystem.logic.Setting

class UserInfoActivity : AppCompatActivity() {

    private lateinit var binding: ActivityUserInfoBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityUserInfoBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setSupportActionBar(binding.toolbarUserInfo)

        setStatusBar()

        initObserve()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun initObserve() {
        Setting.user.observe(this, Observer { user ->
            binding.ivUserProfile2.setImageResource(user.profile)
            binding.tvUserName.text = user.name
            binding.tvUserId.text = String.format(resources.getString(R.string.user_id), user.id)
            if (user.gender == "男") {
                binding.ivUserGender.setImageResource(R.drawable.ic_male)
            } else if (user.gender == "女") {
                binding.ivUserGender.setImageResource(R.drawable.ic_female)
            }
            binding.tvUserGender.text = user.gender
            binding.tvUserAge.text = user.age.toString()

            binding.tvUserEmail.text = user.email
            binding.tvUserPhone.text = user.phone
        })
    }

    // 使状态栏透明
    private fun setStatusBar() {
//        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
//                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        window.statusBarColor = resources.getColor(R.color.green)
    }
}